package com.rzn.reading.controller;


import com.rzn.reading.JsonResult;
import com.rzn.reading.model.dto.note.NoteCreateInputDTO;
import com.rzn.reading.model.dto.note.NoteDetailDTO;
import com.rzn.reading.model.dto.note.NoteUpdateDTO;
import com.rzn.reading.model.dto.note.QueryNoteDTO;
import com.rzn.reading.service.NoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 笔记表 前端控制器
 * </p>
 *
 * @author rzn
 * @since 2022-02-12
 */
@RestController
@RequestMapping("//note")
@Api(tags = "笔记")
public class NoteController {
    @Autowired
    private NoteService noteService;

    @PostMapping("/insert")
    @ApiOperation("添加笔记")
    public JsonResult insertNote(@RequestBody NoteCreateInputDTO noteCreateInputDTO){
        Boolean res = noteService.createNote(noteCreateInputDTO);
        if (res){
            return JsonResult.success("添加成功");
        }else {
            return JsonResult.success("添加失败");
        }
    }

    @PostMapping("/getNoteByPage")
    @ApiOperation("分页模糊查询笔记")
    public JsonResult querySelfNoteByPage(@RequestBody @Validated QueryNoteDTO queryNoteDTO){
        Map<String, Object> noteItemList = noteService.getNoteItemList(queryNoteDTO);
        return JsonResult.success(noteItemList);
    }

    @GetMapping("/updateNoteState/{id}/{state}")
    @ApiOperation("更新笔记状态")
    public JsonResult updateNoteState(@PathVariable Integer id,@PathVariable Integer state){
        Boolean res = noteService.updateNoteState(id, state);
        if (res){
            return JsonResult.success();
        }else {
           return JsonResult.error();
        }
    }

    @GetMapping("/getNoteDetail/{id}")
    @ApiOperation("查询笔记详情")
    public JsonResult queryNoteDetail(@PathVariable Integer id){
        NoteDetailDTO noteDetail = noteService.getNoteDetail(id);
        return JsonResult.success(noteDetail);
    }

    @PostMapping("/updateNote")
    @ApiOperation("更新某个笔记")
    public JsonResult updateNote(@RequestBody NoteUpdateDTO noteUpdateDTO){
        Boolean res = noteService.updateNote(noteUpdateDTO);
        if (res){
            return JsonResult.success();
        }else {
            return JsonResult.error();
        }
    }

    @DeleteMapping("/deleteNote/{id}/{userId}")
    @ApiOperation("删除某个笔记")
    public JsonResult deleteNote(@PathVariable Integer id,@PathVariable Integer userId){
        Boolean res = noteService.deleteNote(id,userId);
        if (res){
            return JsonResult.success();
        }else {
            return JsonResult.error();
        }
    }
}

